package com.dmjj.log.service.impl;

import com.dmjj.log.dao.SysLogMapper;
import com.dmjj.log.pojo.SysLog;
import com.dmjj.log.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import java.sql.*;

@Service
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    private SysLogMapper sysLogMapper;

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        return DriverManager.getConnection("jdbc:sqlserver://192.168.1.74;DatabaseName=testdb", "sa", "admin");
    }

    @Override
    public int saveLogger(SysLog sysLog) {
//        int i = sysLogMapper.saveLogger(sysLog);
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet res = null;
        String sql = "INSERT INTO thislog (Operation,Method,Params,Ip,MenuIds,CreateDate)" +
                " VALUES " +
                "('"+sysLog.getOperation()+"','"+
                sysLog.getMethod()+"','"+
                sysLog.getParams()+"','"+
                sysLog.getIp()+"',"+
                sysLog.getMenuIds()+",getDate())";
        try {
            conn = getConnection();
            stat = conn.prepareStatement(sql);
            stat.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.closeConnection(conn);
            JdbcUtils.closeStatement(stat);
            JdbcUtils.closeResultSet(res);
        }
        return 0;
    }
}
